home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / fviewsrc.zip / FVIEW.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  35KB  |  1,101 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /* Copyright 1989, Doug Boone.  FidoNet 119/5                               */
  4. /*                              (916) 893-9019 Data                         */
  5. /*                              (916) 891-0748 voice                        */
  6. /*                              P.O. Box 5108, Chico, CA. 95928             */
  7. /*                                                                          */
  8. /* This program is not for sale. It is for the free use with Opus systems.  */
  9. /* You may not sell it in ANY way. If you have an access charge to your     */
  10. /* Bulletin Board, consider this to be like Opus, you can ONLY make it      */
  11. /* available for download in an open area, where non-members can get access */
  12. /*                                                                          */
  13. /* If you need to modify this source code, please send me a copy of the     */
  14. /* changes you've made so that everyone can share in the updates.           */
  15. /*                                                                          */
  16. /* "Don't rip me off!" -- Tom Jennings, FidoNet's founder                   */
  17. /*                                                                          */
  18. /*--------------------------------------------------------------------------*/
  19.  
  20. #include    <stdio.h>
  21. #include    <string.h>
  22. #include    <ctype.h>
  23. #include    <stdlib.h>
  24. #include    <time.h>
  25. #include    <dos.h>
  26. #ifdef TURBOC
  27. #include    <dir.h>
  28. #include    <alloc.h>
  29. #else
  30. #include    <malloc.h>
  31. #endif
  32. #include    <io.h>
  33. #include    <fcntl.h>
  34. #include    <dos.h>
  35. #include    <conio.h>
  36. #include    <errno.h>
  37. #include    <sys\types.h>
  38. #include    <sys\stat.h>
  39. #include    <opus.h>
  40. #include    <process.h>
  41. #include    <signal.h>
  42. #include    "archdr.h"
  43. #include    "fview.h"
  44.  
  45. extern  int unzip(char *,char *);
  46. extern    int    unpak(char *,char *);
  47.  
  48. void main(int argc, char *argv[])
  49. {
  50.     int     i;
  51.     char    *result;
  52.     char    Log_path[MAX_PATH];
  53.     long    now;
  54.  
  55. #ifdef TURBOC
  56.     struct  dfree  *space;
  57. #else
  58.     struct  diskfree_t  space;
  59. #endif
  60.  
  61.     signal(SIGINT,handler);        /* Set the ^C handler to internal */
  62.     tzset();         /* We're going to play with the time so..... */
  63.     *Sys_Path = EOS;
  64.     sdisplay = locputs;        /* set ptrs to local functions */
  65.     get_a_char = getch;
  66.     chk_keyboard = kbhit;
  67.     time(&quit_time);        /* Get the current time */
  68. #ifdef  TDEBUG
  69.     quit_time += 1800L;        /* 30 minutes while debugging */
  70. #else
  71.     quit_time += 300L;        /* Otherwise, five minutes */
  72. #endif
  73.     strcpy(dearc_path,"C:\\Dearc\\");        /* Set defaults */
  74.     strcpy(Log_path,"Fview");
  75.  
  76.     for (i=1; i<argc; i++) {
  77.         result = strupr(argv[i]);
  78.         if ((result = strstr(argv[i],".CTL")) != NULL) {
  79.             flags |= OPUS_10;
  80.             result--;
  81.             if (isxdigit(*result))
  82.                 if (isdigit(*result))
  83.                     task = *result -'0';
  84.                 else
  85.                     task = *result - 'A' + 10;
  86.             else
  87.                 task = 0;
  88. #ifdef SDEBUG
  89.             fprintf(Log_fp,"FVIEW Task = %d  %s\n",task,result);
  90.             printf("Task = %d  Result = %s\n",task,result);
  91. #endif
  92.             }
  93.         result = strupr(argv[i]);
  94. #ifdef SDEBUG
  95.         printf("%03i   %s\n",i,argv[i]);
  96.         fprintf(Log_fp,"FVIEW Arg: %2d  %s\n",i,argv[i]);
  97. #endif
  98.  
  99.         if (*result == '-' ||
  100.             *result == '/') {
  101.             result++;
  102.             switch(toupper(*result)) {
  103.  
  104.                 case 'B':   baud = atoi(++result);
  105.                             flags |= FOSSIL;
  106.                             break;
  107.  
  108.                 case '0':   flags |= OPUS_10;
  109.                             break;
  110.  
  111.                 case 'D':    strcpy(dearc_path,++result);
  112.                             break;
  113.  
  114.                 case 'L':    strcpy(Log_path,++result);
  115.                             break;
  116.  
  117.                 case 'S':   strcpy(Sys_Path,++result);
  118.                             break;
  119.  
  120.                 case 'G':    flags |= DEBUG;
  121.                             break;
  122.  
  123.                 case 'N':    flags |= NO_SHOW;
  124.                             break;
  125.  
  126.                 case 'P':   port = atoi(++result);
  127.                             flags |= FOSSIL;
  128.                             break;
  129.  
  130.                 case 'T':    task = atoi(++result);
  131.                             break;
  132.  
  133.                 case 'W':    flags |= WATCH;
  134.                             break;
  135.  
  136.                 }        /* End of switch */
  137.             }        /* End of reading params */
  138.         }        /* End of first for() loop searching for params */
  139.     check_cfg(Log_path);
  140.     result = getenv("FVIEW");
  141.     if (*result != EOS)
  142.         parse_env(result,Log_path);
  143.     strcat(Log_path,".Log");
  144.     Log_fp = fopen(Log_path,"at");
  145.     time(&now);
  146.     fprintf(Log_fp,"FVIEW Coming online %s",ctime(&now));
  147.     if (flags & DEBUG)  {
  148.         for (i=0;i<argc;i++)
  149.             fprintf(Log_fp,"ARG %02d = %s\n",i,argv[i]);
  150. #ifdef TURBOC
  151.         fprintf(Log_fp,"Free Memory = %u\n",coreleft());
  152. #else
  153.         fprintf(Log_fp,"Free Memory = %u\n",_memavl());
  154. #endif
  155.         }
  156.  
  157.     if ((i = strlen(Sys_Path)) > 0) {
  158.         if (Sys_Path[--i] != '\\')
  159.             strcat(Sys_Path,"\\");
  160.         }
  161.  
  162.     i = strlen(dearc_path)-1;
  163.     if (dearc_path[i] == '\\')
  164.         dearc_path[i] = EOS;
  165.  
  166.             /* Check to see if there's actually free disk space */
  167.  
  168. #ifdef TURBOC
  169.     space = (struct dfree *) malloc(sizeof(struct dfree));
  170.     getdfree(dearc_path[0]-'@',space);
  171.     dearc_free = space->df_avail;
  172.     dearc_free *= space->df_bsec;
  173.     dearc_free *= space->df_sclus;
  174.     dearc_free /= 1024L;
  175.     free(space);
  176. #else
  177.     if (_dos_getdiskfree(dearc_path[0]-'@',&space) == 0) {
  178.         dearc_free = (long) space.avail_clusters;
  179.         dearc_free *= (long) space.sectors_per_cluster;
  180.         dearc_free *= (long) space.bytes_per_sector;
  181.         }
  182. #endif
  183.  
  184.     if (flags & DEBUG)
  185.         fprintf(Log_fp,"Free space in dearc directory = %ld\n",dearc_free);
  186.     if (task > 1) {
  187.         sprintf(apath,"%s\\%02X",dearc_path,task);
  188.         strcpy(dearc_path,apath);
  189.         }
  190.     if (flags & DEBUG)
  191.         fprintf(Log_fp,"FVIEW Dearc path = %s\n",dearc_path);
  192.  
  193.     if ((i = access(dearc_path,04)) != 0) {
  194.         fprintf(Log_fp,"FVIEW ERROR Can't access %s Exitting\n",dearc_path);
  195.         sdisplay("\nSorry, Sysop hasn't configured this properly yet.\n");
  196.         terminate(5);
  197.         }
  198.  
  199.     if (flags & OPUS_10)
  200.         get_path("lastuse");
  201.     else
  202.         get_path("lastus");
  203.  
  204.     if (flags & FOSSIL)
  205.         load_fossil();
  206.  
  207. /*--------------------------------------------------------------------------*/
  208. /* This is the loop where the actual work is done. Everything until here    */
  209. /* was just initialization.                                                 */
  210. /*--------------------------------------------------------------------------*/
  211.     do {
  212.         line %= (screen -2);
  213.         i = get_names();
  214.         if (quit_time < time(NULL))
  215.             ctrl_err = 0;
  216.         } while (i && ctrl_err);
  217.     terminate(0);
  218. }
  219.  
  220.  
  221. /*--------------------------------------------------------------------------*/
  222. /* Get the current file area path                                           */
  223. /*--------------------------------------------------------------------------*/
  224.  
  225. void get_path(char *result)
  226. {
  227.     struct  _sys    *system;
  228.     FILE    *infp;
  229.     char    luser[MAX_PATH];
  230.  
  231.     system = (struct _sys *) malloc(sizeof(struct _sys));
  232.  
  233.     if (task == 0)
  234.         sprintf(luser,"%sLASTUSER.BBS",Sys_Path);
  235.     else {
  236.         if (flags & OPUS_10)
  237.             sprintf(luser,"%s%s%X.BBS",Sys_Path,result,task);
  238.         else
  239.             sprintf(luser,"%s%s%02X.BBS",Sys_Path,result,task);
  240.         }
  241.  
  242.     if (flags & DEBUG)
  243.         fprintf(Log_fp,"Reading %s\n",luser);
  244.     if ((infp = fopen(luser,"rb")) == NULL) {
  245.         fprintf(Log_fp,"Can't open lastuser.bbs %s\n",luser);
  246.         fclose(infp);
  247.         terminate(1);
  248.         }
  249.     fread(last,sizeof(struct _usr),1,infp);
  250.     fclose(infp);
  251.     fprintf(Log_fp,"FVIEW User = %s\n",last[0].name);
  252.     screen = (int)last[0].len;
  253.  
  254.     if (last[0].help == 0x06) {
  255.         help();
  256.         if (!(flags & NO_SHOW))
  257.             list_help();
  258.         }
  259.  
  260.     if (last[0].Bits & MORE_PROMPT)
  261.         flags |= WANTS_MORE;
  262.  
  263.     if (last[0].files >0) {
  264.         if (flags & OPUS_10)
  265.             sprintf(apath,"SYSTEM%d.BBS",last[0].files);
  266.         else
  267.             sprintf(apath,"SYSTEM%X.BBS",last[0].files);
  268.         }
  269.     else
  270.         strcpy(apath,"SYSTEM.BBS");
  271.     strcat(Sys_Path,apath);
  272.     if ((infp = fopen(Sys_Path,"rb")) == NULL)
  273.         terminate(2);
  274.     fread(system,sizeof(struct _sys),1,infp);
  275.     fclose(infp);
  276.     strcpy(file_path,system->filepath);
  277.     fprintf(Log_fp,"FVIEW Area = %03d  FilePath = %s\n",last[0].files,file_path);
  278.     free(system);
  279.     return;
  280. }
  281.  
  282. /*--------------------------------------------------------------------------*/
  283. /* If user is on remotely, load the FOSSIL                                  */
  284. /*--------------------------------------------------------------------------*/
  285.  
  286. void load_fossil(void)
  287. {
  288. #ifdef SDEBUG
  289.     printf("Loading FOSSIL\n");
  290. #endif
  291.     if (flags & FOSSIL) {
  292.         if (CkFossilCD()) {
  293.             GetDriverInfo();
  294.             Comm_Purge_out();
  295.             Comm_Purge_in();
  296.             if (flags & WATCH)
  297.                 Comm_Watch(1);        /* turn FOSSIL watchdog on */
  298.             sdisplay = fosputs;        /* set ptrs to fossil functions */
  299.             get_a_char = Comm_Receive;
  300.             chk_keyboard = Comm_Char_Avail;
  301.             }        /* End of succesful FOSSIL install */
  302.         }        /* End of installing FOSSIL */
  303. }
  304.  
  305. /* ====================================================================
  306.  * common termination routine
  307.  * restore environment and exit to dos
  308.  * ====================================================================
  309.  */
  310. void terminate(int stat)
  311. {
  312.     char    temp[80];
  313.     long    now;
  314.  
  315.     flags &= ~MORE;
  316.     sprintf(temp,"\n\n Sysop of 119/5 Thanks you for using FView v. 06.10 : %s\n\n",__DATE__);
  317.     if (!(flags & FOSSIL))
  318.         sdisplay(temp);
  319.     unlink(mbr_path);
  320.     if(flags & FOSSIL) {
  321.         if (Comm_CD())
  322.             sdisplay(temp);
  323.         Comm_Purge_out();
  324.         Comm_Purge_in();
  325.         if (flags & WATCH)
  326.             Comm_Watch(0);        /* turn FOSSIL watchdog off */
  327.         }
  328.     time(&now);
  329.     fprintf(Log_fp,"FVIEW Exiting(%u) %s\n",stat,ctime(&now));
  330.     fclose(Log_fp);
  331.     signal(SIGINT,SIG_DFL);
  332.     exit(stat);
  333. }
  334.  
  335. /*--------------------------------------------------------------------------*/
  336. /* Look at each matching archive file, get full names                       */
  337. /*--------------------------------------------------------------------------*/
  338.  
  339. void file_name(char *start)
  340. {
  341.     int     done;
  342.     int     success;
  343.     long    now;
  344. #ifdef TURBOC
  345.     struct  ffblk   ffblk;
  346. #else
  347.     struct  find_t  cfile;
  348. #endif
  349.     char    name[MAX_PATH];
  350.     char    temp_path[MAX_PATH];
  351.     int     infile;        /* file handle */
  352.     char    *firstchar;
  353.     char    *result;
  354.  
  355.     if (flags & DEBUG)
  356.         fprintf(Log_fp,"In filename(): %s\n",start);
  357.  
  358.     if ((firstchar = (char *) malloc(2)) == NULL) {
  359.         fprintf(Log_fp,"Failed to allocate memory for FIRSTCHAR\n");
  360.         return;
  361.         }
  362.     result = strrchr(start,'\\');
  363.     if (result != NULL)
  364.         *++result = '\0';
  365.     else if ((result = strrchr(start,':')) != NULL)
  366.             *++result = '\0';
  367.     else result = start;
  368.     if (strncmp(result,".",1)==0)
  369.         strcpy(result,"*.*");
  370.     strcpy(temp_path,file_path);
  371.     strcpy(apath,file_path);
  372.     strcat(temp_path,result);
  373. #ifdef TURBOC
  374.     done = findfirst(temp_path,&ffblk,0);
  375. #else
  376.     done = _dos_findfirst(temp_path,_A_NORMAL,&cfile);
  377. #endif
  378.     while (ctrl_err && done == 0) {
  379.         strcpy(name,file_path);
  380.  
  381. #ifdef TURBOC
  382.         strcat(name,ffblk.ff_name);
  383. #else
  384.         strcat(name,cfile.name);
  385. #endif
  386.         time(&now);
  387.         fprintf(Log_fp,"FVIEW %s %s",name,ctime(&now));
  388.         if (flags & FOSSIL && !(Comm_CD()))
  389.             terminate(3);
  390.         infile = open(name,O_RDONLY|O_BINARY);
  391.         if (infile > 0)
  392.             success = read(infile,firstchar,2);
  393.         close(infile);
  394.         flags &= ~0xf000;
  395.         switch(toupper(firstchar[0])) {
  396.             case 0x1a   :   flags |= ARC;        /* Get archive members */
  397.                                success = lstarc(name);
  398.                                                 /* Get text inside archive */
  399.                             if ((success == 0) && (flags & VIEW) &&
  400.                                 !(flags & NO_SHOW))
  401.                                 success = get_mbr_name(name);
  402.                             break;
  403.  
  404.             case 'Z'    :   flags |= ZOO;
  405.                             success = do_zoo(name);
  406.                             break;
  407.  
  408.             case 'P'    :   flags |= ZIP;
  409.                             success = do_zip(name);
  410.                             if (success == 0 && flags & VIEW &&
  411.                                 !(flags & NO_SHOW))
  412.                                 success = get_mbr_name(name);
  413.                             break;
  414.  
  415.             case 'M'    :   play("%s is an EXE file, not an archive\n",
  416. #ifdef TURBOC
  417.                                 ffblk.ff_name);
  418. #else
  419.                                 cfile.name);
  420. #endif
  421.                             break;
  422.  
  423.             default     :   flags |= DWC|LZH;
  424.                             success = read_lzh(name);
  425.                             if (success < 0)
  426.                                 success = read_dwc(name);
  427.                             break;
  428.             }
  429.         if (success < 0) {
  430. #ifdef TURBOC
  431.             sprintf(temp_path,"\n%s Not an archive file\n\n",ffblk.ff_name);
  432. #else
  433.             sprintf(temp_path,"\n%s Not an archive file\n\n",cfile.name);
  434. #endif
  435.             sdisplay(temp_path);
  436.             }
  437. #ifdef TURBOC
  438.         done = findnext(&ffblk);
  439. #else
  440.         done = _dos_findnext(&cfile);
  441. #endif
  442.         if (FOSSIL & flags && !(Comm_CD()))
  443.             done = 1;
  444.         if (success == -1 && ctrl_err == 0) {
  445.             ctrl_err = 1;
  446.             done = 1;
  447.             }
  448.         if (!(ctrl_err))
  449.             done = 1;
  450.         }        /* end of findfirst/findnext loop */
  451.     free(firstchar);
  452.     return;
  453. }
  454.  
  455. /*--------------------------------------------------------------------------*/
  456. /* Get archive/file name(s) from user                                       */
  457. /*--------------------------------------------------------------------------*/
  458.  
  459. int get_names(void)
  460. {
  461.     char    *temp;
  462.     char    *buf;
  463.     char    fname[50];
  464.  
  465.     if (flags & DEBUG)
  466.         fprintf(Log_fp,"Getting names, File-Path = %s\n",file_path);
  467.  
  468.     temp = (char *) malloc(50);
  469.     buf = temp;
  470.     *mbr = EOS;
  471.     memset(temp,'\0',50);
  472.     ctrl_err = 1;
  473.     if (time(NULL) > quit_time)
  474.         terminate(1);
  475.     sdisplay("\n\n  Contents of which file? [ ? for help]  ");
  476.     get_string(temp);
  477.  
  478.     if ((strlen(temp) == 0) ||
  479.         (stricmp(temp,"quit") == 0)) {
  480.         free(temp);
  481.         return(0);
  482.         }
  483.  
  484.     if (flags & WANTS_MORE)
  485.         flags |= MORE;
  486.  
  487.     line %= (screen-2);
  488.  
  489.     if (stricmp(temp,"help") == 0) {
  490.         help();
  491.         if (!(flags & NO_SHOW))
  492.             list_help();
  493.         return(1);
  494.         }
  495.  
  496.     if (strlen(temp) == 1) {
  497.         switch(*temp) {
  498.             case 'Q':   free(temp);return(0);
  499.  
  500.             case 'V':
  501.             case 'L':   flags |= VIEW;
  502.                         file_name(++buf);
  503.                         ctrl_err = 1;
  504.                         return(1);
  505.  
  506.             case 'H':
  507.             case '?':
  508.             default :   help();
  509.                         if (!(flags & NO_SHOW))
  510.                             list_help();
  511.                         return(1);
  512.             }
  513.         }
  514.     buf = strchr(temp,' ');
  515.     if (buf != NULL) {
  516.         if (temp[1] == ' ') {
  517.             if (*temp == 'V' || *temp == 'L') {
  518.                 flags |= VIEW;
  519.                 strcpy(fname,++buf);
  520.                 if ((buf=strchr(fname,' ')) != NULL) {
  521.                     *buf = EOS;
  522.                     strcpy(mbr,++buf);
  523.                     }
  524.                 file_name(fname);
  525.                 ctrl_err = 1;
  526.                 return(1);
  527.                 }
  528.             }
  529.         else {
  530.             flags |= VIEW;
  531.             *buf = EOS;
  532.             ++buf;
  533.             strcpy(mbr,buf);
  534.             file_name(temp);
  535.             ctrl_err = 1;
  536.             return(1);
  537.             }
  538.         }
  539.     else {
  540.         flags &= ~VIEW;
  541.         file_name(temp);
  542.         ctrl_err = 1;
  543.         }
  544.     return(1);
  545. }
  546.  
  547. /*--------------------------------------------------------------------------*/
  548. /* Control Break handler                                                    */
  549. /*--------------------------------------------------------------------------*/
  550.  
  551. void handler(void)
  552. {
  553.  
  554.     signal(SIGINT,SIG_IGN);
  555.     ctrl_err = 0;
  556.     if (flags & FOSSIL) {
  557.         Comm_Purge_in();
  558.         Comm_Purge_out();
  559.         }
  560.     signal(SIGINT,handler);
  561. }
  562.  
  563. void help(void)
  564. {
  565.     sdisplay("\n\n This program will list the contents of any of the existing archive\n");
  566.     sdisplay(" format.  You need to specify a file name, and wildcards will be allowed.\n");
  567.     sdisplay(" Pres <ENTER> or type 'quit' when done and you'll be returned to the BBS.\n\n");
  568.     if (time(NULL) > quit_time)
  569.         terminate(1);
  570.     return;
  571. }
  572.  
  573. void list_help(void)
  574. {
  575.     sdisplay(" If you want to see a text file WITHIN an archive, start the line with a \n");
  576.     sdisplay(" 'V' followed by a space character, then the file name. You'll be prompted\n");
  577.     sdisplay(" for a member name. Only '*'-type wildcards here. Or, you can give an archive\n");
  578.     sdisplay(" name and the internal files you'd like listed.  Something like this:\n\n");
  579.     sdisplay("        Contents of which file?  V *.ARC\n        Contents of which file? ARCVIEW.* ARCVIEW.C\n");
  580.     sdisplay("        Contents of which file?  *.ARC *.DOC\n        Contents of which file? *.* READ.ME\n\n");
  581.     if (time(NULL) > quit_time)
  582.         terminate(1);
  583.     return;
  584. }
  585.  
  586. /*--------------------------------------------------------------------------*/
  587. /* Print out the list of file names inside an archive that's already been   */
  588. /* read in. Used for getting the name of the text file to display.          */
  589. /*--------------------------------------------------------------------------*/
  590.  
  591. void short_list(char *infile)
  592. {
  593.     int     i = 0;
  594.  
  595.     ctrl_err = 1;
  596.     if (time(NULL) > quit_time)
  597.         terminate(1);
  598.     play("\nList of files in %s\n\n",infile);
  599.     do {
  600.         play(" %14s %5ik    ",member[i].name,member[i].size);
  601.         i++;
  602.         if ((i % 3) == 0)
  603.             sendbyte('\n');
  604.         } while (i <= mem_used);
  605.     sendbyte('\n');
  606.     return;
  607. }
  608. /*--------------------------------------------------------------------------*/
  609. /* Get the name of a member of an archive from a user                       */
  610. /*--------------------------------------------------------------------------*/
  611.  
  612. int get_mbr_name(char *name)
  613. {
  614.     char    *buf;
  615.     char    *get_file;
  616.     int     i;
  617.     int     test = 0;
  618.  
  619.     if (!(flags & ARC || flags & ZIP)) {
  620.         sdisplay("\nSorry, Can't handle this type of file.\n\n");
  621.         return(1);
  622.         }
  623.  
  624.     get_file = (char *)malloc(20);
  625.     do {
  626.         if (time(NULL) > quit_time)
  627.             terminate(1);
  628.         if (strlen(mbr) == 0) {
  629.             short_list(name);
  630.             sdisplay("\nWhich file?  [QUIT to stop. ? for help]   ");
  631.             get_string(get_file);
  632.             }
  633.         else
  634.             strcpy(get_file,mbr);
  635.  
  636.         if (strlen(get_file) < 1) {
  637.             free(get_file);
  638.             ctrl_err = 1;
  639.             return(1);
  640.             }
  641.         else if ((strstr(get_file,".COM")) != NULL ||
  642.             (strstr(get_file,".ARC")) != NULL ||
  643.             (strstr(get_file,".ZIP")) != NULL ||
  644.             (strstr(get_file,".PAK")) != NULL ||
  645.             (strstr(get_file,".ZOO")) != NULL ||
  646.             (strstr(get_file,".EXE")) != NULL ||
  647.             (strstr(get_file,".OBJ")) != NULL) {
  648.             play("Can't list %s\n",get_file);
  649.             free(get_file);
  650.             return(1);
  651.             }
  652.         else if (strcmp(get_file,"QUIT") == 0) {
  653.             free(get_file);
  654.             ctrl_err = 0;
  655.             return(0);
  656.             }
  657.         else if (strcmp(get_file,"HELP") == 0) 
  658.              list_help();
  659.              
  660.         else if (strchr(get_file,'*') != NULL) {
  661.             if ((test = do_wild(name,get_file)) == -1) {
  662.                 free(get_file);
  663.                 return(0);
  664.                 }
  665.             else
  666.                 continue;
  667.             }
  668.         else {
  669.             for (i=0;i<=mem_used;i++) {
  670.                 ctrl_err = 1;
  671.                 if (time(NULL) > quit_time) {
  672.                     ctrl_err = 0;
  673.                     return(0);
  674.                     }
  675.                 if (validate(get_file,member[i].name)) {
  676.                     test++;
  677.                     show_file(name,member[i].name);
  678.                     ctrl_err = 1;
  679.                     }
  680.                 }
  681.             *mbr = EOS;
  682.             *get_file = EOS;
  683.             }        /* No wildcards */
  684.         } while (get_file && ctrl_err);
  685.  
  686.     if (test == 0) {
  687.         if ((buf = strrchr(name,'\\')) != NULL)
  688.             play("No matches found in %s\n",buf);
  689.         else
  690.             play("No matches found in %s\n",name);
  691.         }
  692.     free(get_file);
  693.     ctrl_err = 1;
  694.     return(1);
  695. }
  696.  
  697. /*--------------------------------------------------------------------------*/
  698. /* Deal with wildcard requests by prompting users for each archive          */
  699. /*--------------------------------------------------------------------------*/
  700.  
  701. do_wild(char *name, char *get_file)
  702. {
  703.  
  704.     int     i;
  705.     int     test = 0;
  706.     char    ch;
  707.     char    *check;
  708.  
  709.     check = strrchr(name,'\\');
  710.     check++;
  711.     for (i=0;i<=mem_used;i++) {
  712.         ctrl_err = 1;
  713.         if (validate(get_file,member[i].name)) {
  714.             test++;
  715.             if (dearc_free > member[i].size) {
  716.                 if (time(NULL) > quit_time)
  717.                     terminate(1);
  718.                 if (strlen(mbr) != 0) {
  719.                     play("\n List %14s in %s?\n\n",member[i].name,check);
  720.                     sdisplay("      Y)ES (default)          N)o             S)top this archive\n");
  721.                     sdisplay("      E)nd wildcards          Q)uit to OPUS    ");
  722.                     if (!ctrl_err)
  723.                         ch = 'E';
  724.                     else
  725.                         ch = get_a_char();
  726.                     sendbyte('\n');
  727.                     switch(ch) {
  728.                         case 'Q':
  729.                         case 'q':     terminate(0);
  730.                                       break;
  731.                         case 'S':
  732.                         case 's':     ctrl_err = 1;
  733.                                       return(-1);
  734.                         case 'E':
  735.                         case 'e':     ctrl_err = 0;
  736.                                       *mbr = EOS;
  737.                                       *name = EOS;
  738.                                       return(-1);
  739.  
  740.                         case 'n':
  741.                         case 'N':     continue;
  742.  
  743.                         default :     show_file(name,member[i].name);
  744.                                       break;
  745.                         }        /* Do we really want to do this? */
  746.                     }
  747.                 else
  748.                     show_file(name,member[i].name);
  749.                 }        /* There's enough free disk space */
  750.             else  {
  751.                 fprintf(Log_fp,"Not enough disk space to unpack %s : %ldK\n",
  752.                     member[i].name,member[i].size);
  753.                 sdisplay("Sorry, can't unpack that file, too large...\n");
  754.                 if (time(NULL) > quit_time)
  755.                     terminate(1);
  756.                 }
  757.             }        /* End of validating file name */
  758.         }        /* End of FOR loop */
  759.     ctrl_err = 0;
  760.     return(test);
  761. }
  762.  
  763. /*--------------------------------------------------------------------------*/
  764. /* Shell to un-archive a file                                               */
  765. /*--------------------------------------------------------------------------*/
  766.  
  767. show_file(char *name,char *member)
  768. {
  769.     int     result = 100;
  770.     char    get_file[MAX_PATH];
  771.  
  772.  
  773.     sdisplay("\nWorking.....\n");
  774.     sprintf(get_file,"%s\\%s",dearc_path,member);
  775.     if (flags & WANTS_MORE)
  776.         flags |= MORE;
  777.     line %= (screen -2);
  778.     ctrl_err = 1;
  779.     if (flags & DEBUG) {
  780.  
  781. #ifdef TURBOC
  782.         fprintf(Log_fp,"Free Memory = %u\n",coreleft());
  783. #else
  784.         fprintf(Log_fp,"Free Memory = %u\n",_memavl());
  785. #endif
  786.         fprintf(Log_fp,"Output = %s\nInput = %s\nMember = %20s  Flag = %04X\n",
  787.             get_file,name,member,flags);
  788.         }
  789.     if (flags & ARC)
  790. #ifdef PAKIT
  791.         result = unpak(name,get_file);
  792. #else
  793.         result = spawnlp(P_WAIT,"pak.exe","pak.exe","ewa",name,get_file,NULL);
  794. #endif
  795.     else if (flags & ZIP)
  796. /*        result = spawnlp(P_WAIT,"pkunzip.exe","pkunzip.exe","-O",name,dearc_path,member," >NUL",NULL);*/
  797.         result = unzip(name,get_file);
  798.     else
  799.         return;
  800.  
  801.     if (result == 0)
  802.         list_file(get_file);
  803.     else {
  804.         play("Error reading %s from %s\n",member,name);
  805.         fprintf(Log_fp,"Error %02d Reading %s from %s\n",result,member,name);
  806.         if (result <0)
  807.             terminate(errno);
  808.         }
  809.     unlink(get_file);
  810.     return;
  811. }
  812.  
  813. /*--------------------------------------------------------------------------*/
  814. /* List the actual text that has been pulled out of the archive             */
  815. /*--------------------------------------------------------------------------*/
  816.  
  817. void list_file(char *name)
  818. {
  819.     char    *one_line;
  820.     char    *result;
  821.     int     count;
  822.     FILE    *infp;
  823.     long    now;
  824.  
  825.     time(&now);
  826.     fprintf(Log_fp,"FVIEW Listing %s %s",name,ctime(&now));
  827.     
  828.     if ((infp = fopen(name,"rt")) != NULL) {
  829.  
  830.         one_line = (char *) malloc(100);
  831.         do {
  832.             count = 0;
  833.             result = one_line;
  834.             do {
  835.                 *result = fgetc(infp);
  836.                 if (*result == '\n' || *result == '\r')
  837.                     count = 81;
  838.                 else if (!isprint(*result))
  839.                     *result = ' ';
  840.                 result++;
  841.                 count++;
  842.                 } while (*result != '\n' && count < 81);
  843.             *result = EOS;
  844.             sdisplay(one_line);
  845.             } while(ctrl_err && result && !feof(infp));
  846.         free(one_line);
  847.         fclose(infp);
  848.         }
  849.     return;
  850. }
  851.  
  852. /*--------------------------------------------------------------------------*/
  853. /* Validate that the name of a member of an archive that a user has asked   */
  854. /* to read actually exists. You can't pass "*.*" to the unarchive program!  */
  855. /*--------------------------------------------------------------------------*/
  856.  
  857. int validate(char *name,char *target)
  858.  
  859. {
  860.     char    *pt1;
  861.     char    *pt2;
  862.     char    real[14];
  863.     char    ext[4];
  864.     int     m1 = 0;
  865.     int     l1;
  866.     int     l2;
  867.  
  868.     if (flags & FOSSIL && !(Comm_CD()))
  869.         terminate(3);
  870.  
  871.     if ((strstr(target,".COM")) != NULL ||
  872.         (strstr(target,".ARC")) != NULL ||
  873.         (strstr(target,".ZIP")) != NULL ||
  874.         (strstr(target,".PAK")) != NULL ||
  875.         (strstr(target,".ZOO")) != NULL ||
  876.         (strstr(target,".EXE")) != NULL ||
  877.         (strstr(target,".OBJ")) != NULL)
  878.             return(0);        /* Can't deal with these */
  879.  
  880.     if (strcmp(name,"*.*") == 0)
  881.         return(1);
  882.  
  883.     if (strcmp(name,target) == 0)
  884.         return(1);
  885.  
  886.     strcpy(real,name);        /* Work on a copy, not the original */
  887.  
  888. /* Match root name */
  889.     if ((pt1 = strchr(real,'.')) != NULL) {        /* Parse file name */
  890.         *pt1 = EOS;
  891.         strcpy(ext,++pt1);
  892.         }
  893.     if ((pt1 = strchr(real,'*')) != NULL) {
  894.         if (real[0] != '*') {
  895.             *pt1 = EOS;
  896.             l1 = strlen(real);
  897.             if (strnicmp(real,target,l1) != 0)
  898.                 return(0);
  899.             else
  900.                 m1 = 1;
  901.             }
  902.         else
  903.             m1 = 1;
  904.         }
  905.     else {
  906.         l1 = strlen(real);
  907.         if (strchr(target,'.') != NULL) {
  908.             l2 = 0;
  909.             pt2 = target;
  910.             while (*pt2++ != '.')
  911.                 l2++;
  912.             }
  913.         else
  914.             l2 = strlen(target);
  915.  
  916.         if (l1 >= l2)
  917.             l1 = l2;
  918.         if (strnicmp(real,target,l1) != 0)
  919.             return(0);
  920.         else
  921.             m1 = 1;
  922.         }        /* End of matching filename */
  923.  
  924. /* Try matching extensions */
  925.     if ((pt1 = strchr(ext,'*')) != NULL) {
  926.         if (ext[0] == '*' && m1 == 1)
  927.             return(1);
  928.         else {
  929.             *pt1 = EOS;
  930.             l1 = strlen(ext);
  931.             if ((pt1 = strchr(target,'.')) != NULL) {
  932.                 pt1++;
  933.                 if ((strnicmp(ext,pt1,l1) == 0) && m1 == 1)
  934.                     return(1);
  935.                 else
  936.                     return(0);
  937.                 }
  938.             if (strlen(ext) == 0 && m1 == 1)
  939.                 return(1);
  940.             else
  941.                 return(0);
  942.             }
  943.         }        /* End of wildcard in Extension */
  944.     else {
  945.         if ((pt1 = strchr(target,'.')) != NULL) {
  946.             pt1++;
  947.             l1 = strlen(pt1);
  948.             if ((strnicmp(ext,pt1,l1) == 0) && m1 == 1)
  949.                 return(1);
  950.             else
  951.                 return(0);
  952.             }
  953.         else
  954.             if (strlen(ext) == 0 && m1 == 1)
  955.                 return(1);
  956.         return(0);
  957.         }
  958. }
  959. /*--------------------------------------------------------------------------*/
  960. /* Get a string of characters from a remote user.                           */
  961. /*--------------------------------------------------------------------------*/
  962.  
  963. void    get_string(char *buf)
  964. {
  965.     char    ch = EOS;
  966.     char    *start;
  967.  
  968.     start = buf;        /* Used to prevent user BS past start of line */
  969.  
  970.     if (!ctrl_err)
  971.         return;
  972.     while((ch = get_a_char()) != '\r') {
  973.         ch = toupper(ch);
  974.         if (ch != 0x08) {
  975.             *buf++ = ch;
  976.             sendbyte(ch);
  977.             }
  978.         else if (buf>start) {
  979.             ch = ' ';
  980.             sendbyte(ch);
  981.             sendbyte(0x08);
  982.             sendbyte(0x08);
  983.             sendbyte(ch);
  984.             sendbyte(0x08);
  985.             *--buf = EOS;
  986.             }
  987.         }
  988.     *buf++ = EOS;
  989.     sendbyte('\n');
  990.     return;
  991. }
  992.  
  993. /*--------------------------------------------------------------------------*/
  994. /* Check for the existence of a CFG file 1.03 people                        */
  995. /*--------------------------------------------------------------------------*/
  996. void    check_cfg(char *Log)
  997. {
  998.     FILE    *cfg;
  999.     char    *result;
  1000.     char    *check;
  1001.     char    *aline;
  1002.  
  1003.     if ((cfg = fopen("FVIEW.CFG","rt")) == NULL)
  1004.         return;
  1005.     aline = (char *) malloc(120);
  1006.     do {
  1007.         result = fgets(aline,80,cfg);
  1008.         check = strrchr(aline,'\n');
  1009.         *check = EOS;
  1010.         if ((check = strchr(aline,';')) != NULL)
  1011.             *check = EOS;
  1012.         if (strlen(aline) > 0) {
  1013.             if (strnicmp(aline,"Log",3) == 0) {
  1014.                 check = get_next(aline);
  1015.                 strcpy(Log,check);
  1016.                 }
  1017.             else if (strnicmp(aline,"dearc",5) == 0) {
  1018.                 check = get_next(aline);
  1019.                 strcpy(dearc_path,check);
  1020.                 }
  1021.             else if (strnicmp(aline,"System",6) == 0) {
  1022.                 check = get_next(aline);
  1023.                 strcpy(Sys_Path,check);
  1024.                 }
  1025.             else if (strnicmp(aline,"noshow",6) == 0) 
  1026.                 flags |= NO_SHOW;
  1027.             else if (strnicmp(aline,"debug",5) == 0)
  1028.                 flags |= DEBUG;
  1029.             else if (strnicmp(aline,"watch",5) == 0)
  1030.                 if (flags & FOSSIL)
  1031.                     flags |= WATCH;
  1032.             }        /* End of getting matches for flags */
  1033.         } while(result != NULL && !feof(cfg));
  1034.     free(aline);
  1035.     fclose(cfg);
  1036.     return;
  1037. }
  1038.  
  1039. /*--------------------------------------------------------------------------*/
  1040. /* Parse lines in CFG file if needed                                        */
  1041. /*--------------------------------------------------------------------------*/
  1042.  
  1043. char *get_next(char *line)
  1044. {
  1045.     char    *test;
  1046.     char    *test1;
  1047.  
  1048.     test = line;
  1049.     while (!isspace(*test) && (++test));
  1050.     while (isspace(*test) && (++test));
  1051.     test1 = test;
  1052.     while (!isspace(*test1) && (++test1));
  1053.     *test1 = EOS;
  1054.     return(test);
  1055. }
  1056. /*--------------------------------------------------------------------------*/
  1057. /* If user has a "SET FVIEW=...." in environment                            */
  1058. /*--------------------------------------------------------------------------*/
  1059. void    parse_env(char *env,char *Log)
  1060. {
  1061.     char    *test1;
  1062.  
  1063.     test1 = env;
  1064.     while(*env && *env > 0x1f) {
  1065.         while (!isspace(*test1) && (++test1));
  1066.         *test1 = EOS;
  1067.         ++test1;
  1068.         if (*env == '-' ||
  1069.             *env == '/') {
  1070.             env++;
  1071.             switch(toupper(*env)) {
  1072.  
  1073.                 case '0':   flags |= OPUS_10;
  1074.                             break;
  1075.  
  1076.                 case 'D':    strcpy(dearc_path,++env);
  1077.                             break;
  1078.  
  1079.                 case 'L':    strcpy(Log,++env);
  1080.                             break;
  1081.  
  1082.                 case 'S':   strcpy(Sys_Path,++env);
  1083.                             break;
  1084.  
  1085.                 case 'G':    flags |= DEBUG;
  1086.                             break;
  1087.  
  1088.                 case 'N':    flags |= NO_SHOW;
  1089.                             break;
  1090.  
  1091.                 case 'W':    flags |= WATCH;
  1092.                             break;
  1093.  
  1094.                 }        /* End of switch */
  1095.             }        /* end of finding '-' */
  1096.         env = test1;
  1097.         }        /* end of while(env) loop */
  1098.     return;
  1099. }
  1100.  
  1101.